package Action.Manager;

import Action.BackgroundThread.RecommendedbooksThread;
import Action.Sys.Setting;
import Bean.BookInfoEntity;
import Bean.LoanBookLogRecord;
import Bean.Log.HistoricalData;
import Bean.UserinfoEntity;
import com.opensymphony.xwork2.ActionSupport;
import org.hibernate.Session;
import tools.HibernateTools;
import tools.TimeTools;

import java.sql.Timestamp;
import java.util.LinkedList;
import java.util.List;


public class Home extends ActionSupport {
    int[] visernum=new int[7];
    int[] ordernums=new int[7];
    int[] newUserNums=new int[7];
    int[] newBookNums=new int[7];
    int[] newCommentNums=new int[30];
    String[] _date=new String[30];
    int[] visernum_moth=new int[30];
    LinkedList<Object[]> loanBookLogRecords;
    long UserNum;
    long BookNum;
    long SysLog;
    long UserLog;
    long orderNuM;//订单总数
    long tadayOrderNum;//今天预约借书数量
    long tadayReturnOrderNum;//今天待还数量
    long cacheSize;
    long clear;


    @Override
    public String execute() throws Exception {
        RecommendedbooksThread.sys_busy=true;
        Session session = HibernateTools.getSession();
        HistoricalData hd;
        for (int i = 0; i < visernum.length; i++) {
            Timestamp timestamp = TimeTools.formatTime(visernum.length - i - 1);
            hd = session.get(HistoricalData.class, timestamp);
            if (hd==null){
                hd = new HistoricalData();
                hd.setTimestamp(TimeTools.formatTime(visernum.length-i-1));
                session.saveOrUpdate(hd);
            }

            visernum[i]= hd.getVisterNum();
            ordernums[i]= hd.getOrderNum();
            newUserNums[i]=hd.getNewUserNum();
            newBookNums[i]=hd.getNewBookNum();
        }
        for (int i = 0; i < _date.length; i++) {
            Timestamp timestamp = TimeTools.formatTime(_date.length - i - 1);
            hd = session.get(HistoricalData.class, timestamp);
            if (hd==null){
                hd = new HistoricalData();
                hd.setTimestamp(TimeTools.formatTime(_date.length-i-1));
                session.saveOrUpdate(hd);
            }
            visernum_moth[i]=hd.getVisterNum();
            newCommentNums[i]= hd.getCommentNum();
            _date[i]=TimeTools.timeStamp2Date(timestamp, "dd");
        }

        try {
            UserNum= (Long) session.createQuery("select count (*) from UserinfoEntity ").list().get(0);
            BookNum= (Long) session.createQuery("select count (*) from BookInfoEntity ").list().get(0);
            SysLog= (Long) session.createQuery("select count (*) from SystemLogEntity ").list().get(0);
            UserLog= (Long) session.createQuery("select count (*) from UserLogEntity ").list().get(0);
            orderNuM= (Long) session.createQuery("select count (*) from LoanBookLogRecord ").list().get(0);
//            tadayOrderNum= (Long) session.createQuery("select count (*) from LoanBookLogRecord where state=?").setParameter(0, Setting.LoanBook_state_reservation).list().get(0);
            tadayOrderNum=ordernums[ordernums.length-1];
            tadayReturnOrderNum= (Long) session.createQuery("select count (*) from LoanBookLogRecord where state=?").setParameter(0, Setting.LoanBook_state_need_TimeOut).list().get(0);
            List<LoanBookLogRecord> middle = session.createQuery("from LoanBookLogRecord where state=?").setParameter(0, Setting.LoanBook_state_need_Return).getResultList();
            for (LoanBookLogRecord loanBookLogRecord : middle) {
                if (loanBookLogRecord.remainingDays()<=0){
                    tadayReturnOrderNum++;
                }
            }
        }catch (Exception e){
            addActionError("服务器异常");
            System.err.println(e.fillInStackTrace());
        }

        //显示最近10条图书订单
        loanBookLogRecords = new LinkedList<>();
        List<LoanBookLogRecord> middle=session.createQuery("from LoanBookLogRecord order by id desc ").setMaxResults(10).getResultList();
        for (LoanBookLogRecord loanBookLogRecord : middle) {
            String id=""+loanBookLogRecord.getId();
            BookInfoEntity bookInfoEntity = session.get(BookInfoEntity.class, loanBookLogRecord.getBookId());
            String bookname= bookInfoEntity==null?"已删除图书ID:"+loanBookLogRecord.getUserId():bookInfoEntity.getBook_name();
            UserinfoEntity userinfoEntity = session.get(UserinfoEntity.class, loanBookLogRecord.getUserId());
            String username= userinfoEntity==null?"已删除用户ID:"+loanBookLogRecord.getUserId():""+loanBookLogRecord.getUserId();
            String orderTime=TimeTools.timeStamp2Date(loanBookLogRecord.getLoanTime(),null);
            String state=loanBookLogRecord.getState();
            String day=""+loanBookLogRecord.getDay();
            Integer statepercentage=0;
            if (state.equals(Setting.LoanBook_state_closed)){
                statepercentage=100;
            }else if (state.equals(Setting.LoanBook_state_reservation))
            {
                statepercentage=25;
            }else if (state.equals(Setting.LoanBook_state_need_TimeOut)||state.equals(Setting.LoanBook_state_need_Return)){
                statepercentage=75;
            }else {
                statepercentage=100;//用户取消等情况
            }

            loanBookLogRecords.addLast(new Object[]{id,username,bookname,orderTime,day,state,statepercentage});

        }

        HibernateTools.commit(session);

        return SUCCESS;
    }

    public int[] getVisernum() {
        return visernum;
    }

    public void setVisernum(int[] visernum) {
        this.visernum = visernum;
    }

    public long getUserNum() {
        return UserNum;
    }

    public void setUserNum(long userNum) {
        UserNum = userNum;
    }

    public long getBookNum() {
        return BookNum;
    }

    public void setBookNum(long bookNum) {
        BookNum = bookNum;
    }

    public long getSysLog() {
        return SysLog;
    }

    public void setSysLog(long sysLog) {
        SysLog = sysLog;
    }

    public long getUserLog() {
        return UserLog;
    }

    public void setUserLog(long userLog) {
        UserLog = userLog;
    }

    public long getOrderNuM() {
        return orderNuM;
    }

    public void setOrderNuM(long orderNuM) {
        this.orderNuM = orderNuM;
    }

    public long getTadayOrderNum() {
        return tadayOrderNum;
    }

    public void setTadayOrderNum(long tadayOrderNum) {
        this.tadayOrderNum = tadayOrderNum;
    }

    public long getTadayReturnOrderNum() {
        return tadayReturnOrderNum;
    }

    public void setTadayReturnOrderNum(long tadayReturnOrderNum) {
        this.tadayReturnOrderNum = tadayReturnOrderNum;
    }

    public long getCacheSize() {
        return cacheSize;
    }

    public void setCacheSize(long cacheSize) {
        this.cacheSize = cacheSize;
    }

    public long getClear() {
        return clear;
    }

    public void setClear(long clear) {
        this.clear = clear;
    }

    public int[] getOrdernums() {
        return ordernums;
    }

    public void setOrdernums(int[] ordernums) {
        this.ordernums = ordernums;
    }

    public int[] getNewUserNums() {
        return newUserNums;
    }

    public void setNewUserNums(int[] newUserNums) {
        this.newUserNums = newUserNums;
    }

    public int[] getNewBookNums() {
        return newBookNums;
    }

    public void setNewBookNums(int[] newBookNums) {
        this.newBookNums = newBookNums;
    }

    public int[] getNewCommentNums() {
        return newCommentNums;
    }

    public void setNewCommentNums(int[] newCommentNums) {
        this.newCommentNums = newCommentNums;
    }

    public LinkedList<Object[]> getLoanBookLogRecords() {
        return loanBookLogRecords;
    }

    public void setLoanBookLogRecords(LinkedList<Object[]> loanBookLogRecords) {
        this.loanBookLogRecords = loanBookLogRecords;
    }


    public String[] get_date() {
        return _date;
    }

    public void set_date(String[] _date) {
        this._date = _date;
    }

    public int[] getVisernum_moth() {
        return visernum_moth;
    }

    public void setVisernum_moth(int[] visernum_moth) {
        this.visernum_moth = visernum_moth;
    }
}
